package org.se.scut.pinterest.mina.codec.filte.coder.decoder;

import java.util.List;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.demux.MessageDecoderResult;
import org.se.scut.pinterest.mina.codec.coder.cxt.AllPictureTypeResultContext;
import org.se.scut.pinterest.mina.codec.coder.cxt.BaseContext;
import org.se.scut.pinterest.mina.codec.msg.AllPictureTypeResultMessage;
import org.se.scut.pinterest.mina.codec.msg.AllPictureTypeResultMessageItem;
import org.se.scut.pinterest.mina.utils.ListUtils;

public class AllPictureTypeResultMessageDecoder extends BaseMessageDecoder {

	public AllPictureTypeResultMessageDecoder() {
		super(new AttributeKey(AllPictureTypeResultMessageDecoder.class, "context" ), "atr");
	}

	@Override
	public MessageDecoderResult decode(IoSession session, IoBuffer in,
			ProtocolDecoderOutput out) throws Exception {
		AllPictureTypeResultContext context = (AllPictureTypeResultContext)getContext(session);
		IoBuffer buffer = context.getInnerBuffer();
		int matchCount = context.getMatchCount();
		int lineNumber = context.getLineNumber();
		String symbol = context.getSymbol();
		String size = context.getSize();
		String id = context.getId();
		String typeName = context.getTypeName();
		String lastModifiedDate = context.getLastModifiedDate();
		List<AllPictureTypeResultMessageItem> items = context.getItems();
		byte b;
		while (in.hasRemaining()) {
			b = in.get();
			matchCount++;
			buffer.put(b);
			if (b == 10) {
				if (lineNumber == 0) {
					buffer.flip();
					symbol = buffer.getString(matchCount, DECODER);
					symbol = symbol.substring(0, symbol.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setSymbol(symbol);
				}
				if (lineNumber == 1) {
					buffer.flip();
					size = buffer.getString(matchCount, DECODER);
					size = size.substring(0, size.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setSize(size);
				}
				if ((lineNumber + 1) % 3 == 0 && lineNumber < 3 * Integer.parseInt(size) + 2) {
					buffer.flip();
					id = buffer.getString(matchCount, DECODER);
					id = id.substring(0, id.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setId(id);
				}
				if (lineNumber != 0
						&& (lineNumber + 0) % 3 == 0 
						&& lineNumber < 3 * Integer.parseInt(size) + 2) {
					buffer.flip();
					typeName = buffer.getString(matchCount, DECODER);
					typeName = typeName.substring(0, typeName.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setTypeName(typeName);
				}
				if (lineNumber != 1 
						&& (lineNumber + 2) % 3 == 0 
						&& lineNumber < 3 * Integer.parseInt(size) + 2) {
					buffer.flip();
					lastModifiedDate = buffer.getString(matchCount, DECODER);
					lastModifiedDate = lastModifiedDate.substring(0, lastModifiedDate.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setLastModifiedDate(lastModifiedDate);
					AllPictureTypeResultMessageItem item = new AllPictureTypeResultMessageItem();
					item.setId(Integer.parseInt(id));
					item.setTypeName(typeName);
					item.setLastModifiedDate(lastModifiedDate);
					items.add(item);
					context.setItems(items);
					context.setLineNumber(lineNumber);
					if (lineNumber == 3 * Integer.parseInt(size) + 1) {
						break;
					}
				}
				lineNumber++;
			}
			context.setMatchCount(matchCount);
			context.setLineNumber(lineNumber);
		}
		if (context.getLineNumber() == 3 * Integer.parseInt(context.getSize()) + 1) {
			AllPictureTypeResultMessage msg = new AllPictureTypeResultMessage();
			ListUtils<AllPictureTypeResultMessageItem> utils = new ListUtils<AllPictureTypeResultMessageItem>();
			utils.copyList(msg.getItems(), context.getItems());
			out.write(msg);
			context.resetData();
			return MessageDecoderResult.OK;
		}else {
			return MessageDecoderResult.NEED_DATA;
		}
	}

	@Override
	public void finishDecode(IoSession session, ProtocolDecoderOutput out)
			throws Exception {

	}

	@Override
	public BaseContext getContext(IoSession session) {
		AllPictureTypeResultContext context = (AllPictureTypeResultContext)session.getAttribute(CONTEXT_KEY);
		if (context == null) {
			context = new AllPictureTypeResultContext();
			session.setAttribute(CONTEXT_KEY, context);
		}
		return context;
	}

}
